﻿using System;
using UnityEngine;
using UnityEngine.Events;

namespace RenderHeads.Media.AVProVideo
{
	// Token: 0x02000019 RID: 25
	[HelpURL("http://renderheads.com/product/avpro-video/")]
	[AddComponentMenu("AVPro Video/Audio Output", 400)]
	[RequireComponent(typeof(AudioSource))]
	public class AudioOutput : MonoBehaviour
	{
		// Token: 0x06000096 RID: 150 RVA: 0x00006380 File Offset: 0x00004580
		private void Awake()
		{
			this._audioSource = base.GetComponent<AudioSource>();
		}

		// Token: 0x06000097 RID: 151 RVA: 0x00006390 File Offset: 0x00004590
		private void Start()
		{
			this.ChangeMediaPlayer(this._mediaPlayer);
		}

		// Token: 0x06000098 RID: 152 RVA: 0x000063A0 File Offset: 0x000045A0
		private void OnDestroy()
		{
			this.ChangeMediaPlayer(null);
		}

		// Token: 0x06000099 RID: 153 RVA: 0x000063AC File Offset: 0x000045AC
		private void Update()
		{
			if (this._mediaPlayer != null && this._mediaPlayer.Control != null && this._mediaPlayer.Control.IsPlaying())
			{
				AudioOutput.ApplyAudioSettings(this._mediaPlayer, this._audioSource);
			}
		}

		// Token: 0x0600009A RID: 154 RVA: 0x00006400 File Offset: 0x00004600
		public void ChangeMediaPlayer(MediaPlayer newPlayer)
		{
			if (this._mediaPlayer != null)
			{
				this._mediaPlayer.Events.RemoveListener(new UnityAction<MediaPlayer, MediaPlayerEvent.EventType, ErrorCode>(this.OnMediaPlayerEvent));
				this._mediaPlayer = null;
			}
			this._mediaPlayer = newPlayer;
			if (this._mediaPlayer != null)
			{
				this._mediaPlayer.Events.AddListener(new UnityAction<MediaPlayer, MediaPlayerEvent.EventType, ErrorCode>(this.OnMediaPlayerEvent));
			}
		}

		// Token: 0x0600009B RID: 155 RVA: 0x00006478 File Offset: 0x00004678
		private void OnMediaPlayerEvent(MediaPlayer mp, MediaPlayerEvent.EventType et, ErrorCode errorCode)
		{
			if (et != MediaPlayerEvent.EventType.Closing)
			{
				if (et == MediaPlayerEvent.EventType.Started)
				{
					AudioOutput.ApplyAudioSettings(this._mediaPlayer, this._audioSource);
					this._audioSource.Play();
				}
			}
			else
			{
				this._audioSource.Stop();
			}
		}

		// Token: 0x0600009C RID: 156 RVA: 0x000064CC File Offset: 0x000046CC
		private static void ApplyAudioSettings(MediaPlayer player, AudioSource audioSource)
		{
			if (player != null && player.Control != null)
			{
				float volume = player.Control.GetVolume();
				bool mute = player.Control.IsMuted();
				float playbackRate = player.Control.GetPlaybackRate();
				audioSource.volume = volume;
				audioSource.mute = mute;
				audioSource.pitch = playbackRate;
			}
		}

		// Token: 0x0600009D RID: 157 RVA: 0x0000652C File Offset: 0x0000472C
		private void OnAudioFilterRead(float[] data, int channels)
		{
			AudioOutputManager.Instance.RequestAudio(this, this._mediaPlayer, data, this._channelMask, channels, this._audioOutputMode);
		}

		// Token: 0x0400008C RID: 140
		public AudioOutput.AudioOutputMode _audioOutputMode = AudioOutput.AudioOutputMode.Multiple;

		// Token: 0x0400008D RID: 141
		[SerializeField]
		private MediaPlayer _mediaPlayer;

		// Token: 0x0400008E RID: 142
		private AudioSource _audioSource;

		// Token: 0x0400008F RID: 143
		[HideInInspector]
		public int _channelMask = -1;

		// Token: 0x0200001A RID: 26
		public enum AudioOutputMode
		{
			// Token: 0x04000091 RID: 145
			Single,
			// Token: 0x04000092 RID: 146
			Multiple
		}
	}
}
